package com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.SystemGiveItem;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ItemViewDelegate;
import com.hanhanxiaochong.zhongxiangchuanmei.config.MultiItemTypeAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.utils.TimeUtils;

import java.util.List;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/30.
 * Desc   :
 */

public class SystemGiveAdapter extends MultiItemTypeAdapter<SystemGiveItem> {

    public SystemGiveAdapter(Context context, List<SystemGiveItem> datas) {
        super(context, datas);

        addItemViewDelegate(new ProjectItemDelegate());
        addItemViewDelegate(new GoodItemDelegate());
        addItemViewDelegate(new SystemItemDelegate());
    }

    class ProjectItemDelegate implements ItemViewDelegate<SystemGiveItem> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_sys_give;
        }

        @Override
        public boolean isForViewType(SystemGiveItem item, int position) {
            return item.getType().equals("project");
        }

        @Override
        public void convert(ViewHolder holder, SystemGiveItem bean, int position) {
            ImageView ivPic = holder.getView(R.id.iv_project_pic);
            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + bean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivPic);
            if (bean.getC_name() != null) {
                holder.setText(R.id.tv_coupon_type, String.format(ResourcesUtils.getString(R.string.project_coupon_type), bean.getC_name()));
            }
            if (bean.getD_name() != null) {
                holder.setText(R.id.tv_coupon_name, bean.getD_name());
            }
            if (bean.getPl_num() != null) {
                holder.setText(R.id.tv_coupon_time, String.format(ResourcesUtils.getString(R.string.project_coupon_frequency), bean.getPl_num()));
            }
            if (bean.getT_name() != null) {
                holder.setText(R.id.tv_coupon_fit, bean.getT_name());
            }
            holder.setText(R.id.tv_coupon_date, String.format(ResourcesUtils.getString(R.string.term), TimeUtils.getDateFromSeconds(bean.getEffective_time())));
        }
    }

    class GoodItemDelegate implements ItemViewDelegate<SystemGiveItem> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_coupon;
        }

        @Override
        public boolean isForViewType(SystemGiveItem item, int position) {
            return item.getType().equals("product");
        }

        @Override
        public void convert(ViewHolder holder, SystemGiveItem bean, int position) {
            TextView money = holder.getView(R.id.tv_coupon_money);
            TextView name = holder.getView(R.id.tv_coupon_name);
            TextView time = holder.getView(R.id.tv_coupon_time);
            String d_discount = bean.getD_discount();
            if (d_discount.equals("0")) {
                money.setText("免费");
            } else {
                money.setText(String.format(ResourcesUtils.getString(R.string.good_price), d_discount));
            }
            name.setText(bean.getD_name());
            time.setText(String.format(ResourcesUtils.getString(R.string.term), TimeUtils.getDateFromSeconds(bean.getEffective_time())));
        }
    }

    class SystemItemDelegate implements ItemViewDelegate<SystemGiveItem> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_sys_give2;
        }

        @Override
        public boolean isForViewType(SystemGiveItem item, int position) {
            return item.getType().equals("systemGive");
        }

        @Override
        public void convert(ViewHolder holder, SystemGiveItem bean, int position) {
            ImageView ivPic = holder.getView(R.id.iv_project_pic);
            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + bean.getPic().split(",")[0], R.mipmap.img_error, R.mipmap.img_error, ivPic);
            if (bean.getName() != null) {
                holder.setText(R.id.tv_coupon_name, bean.getName());
            }
            if (bean.getPrice_vip() != null) {
                holder.setText(R.id.tv_project_vip_price, String.format(ResourcesUtils.getString(R.string.vip_good_price), bean.getPrice_vip()));
            }
            if (bean.getPrice() != null) {
                holder.setText(R.id.tv_project_normal_price, String.format(String.format(ResourcesUtils.getString(R.string.normal_good_price), bean.getPrice())));
            }
            if (bean.getWhen_long() != null) {
                holder.setText(R.id.tv_project_time, bean.getWhen_long() + "分钟");
            }
        }
    }

}
